#!/bin/bash

#            ---------------------------------------------------
#                              Ghost Framework                                                  
#            ---------------------------------------------------
#                Copyright (C) <2019-2020>  <Entynetproject>
#
#        This program is free software: you can redistribute it and/or modify
#        it under the terms of the GNU General Public License as published by
#        the Free Software Foundation, either version 3 of the License, or
#        any later version.
#
#        This program is distributed in the hope that it will be useful,
#        but WITHOUT ANY WARRANTY; without even the implied warranty of
#        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#        GNU General Public License for more details.
#
#        You should have received a copy of the GNU General Public License
#        along with this program.  If not, see <http://www.gnu.org/licenses/>.

G="\033[1;34m[*] \033[0m"
E="\033[1;31m[-] \033[0m"
S="\033[1;32m[+] \033[0m"

if [[ "$1" = "" ]]; then
    echo "Usage: download {hidden}"
    echo
    echo "Note: This file only for Ghost Framework, if you execute it without"
    echo "Ghost Framework it will not work, this is only a part of main code."
    exit
fi

if [[ "$2" = "" ]]; then
    echo "Usage: download {hidden}"
    echo
    echo "Note: This file only for Ghost Framework, if you execute it without"
    echo "Ghost Framework it will not work, this is only a part of main code."
    exit
fi
    
if [[ "$3" = "" ]]; then
    echo "Usage: download {hidden}"
    echo
    echo "Note: This file only for Ghost Framework, if you execute it without"
    echo "Ghost Framework it will not work, this is only a part of main code."
    exit
else
    {
    adb shell stat $1
    } &> .check_remote_path_exists
    {
    check_remote_file_exists="$(cat .check_remote_file_exists)"
    rm .check_remote_file_exists
    } &> /dev/null
    {
    dchk="$(adb shell """if [[ -d """$1""" ]]
    then
    echo 0
    fi""")"
    } &> /dev/null
    if [[ "$check_remote_path_exists" = "stat: '"$1"': No such file or directory" ]]; then
        echo -e ""$E"Remote file: "$1": does not exist!"
    else
        if [[ "$dchk" = "0" ]]; then
            echo -e ""$E"Error: "$1": not a file!"
        else
            if [[ "$4" = "" ]]; then
                echo -e ""$G"Downloading "$3"..."
            else
                echo -e ""$G"Downloading "$4"..."
            fi
            {
            adb pull $1 $2
            } &> /dev/null
            if [[ "$4" = "" ]]; then
                ci=$2
                if [[ "${ci[@]: (-1)}" = "/" ]]; then
                    echo -e ""$G"Saving to "$2""$3"..."
                    sleep 1
                    echo -e ""$S"Saved to "$2""$3"!"
                else
                    echo -e ""$G"Saving to "$2"/"$3"..."
                    sleep 1
                    echo -e ""$S"Saved to "$2"/"$3"!"
                fi
            else
                echo -e ""$G"Saving to "$2"..."
                sleep 1
                echo -e ""$S"Saved to "$2"!"
            fi
        fi
    fi
fi
